#include "Sock.h"
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <strings.h>

#include "InetAddress.h"
#include "Logger.h"

Socket::~Socket()
{
    close(sockfd_);
}

int Socket::bindAddress(const InetAddress &localAddr)
{
    if(0 != bind(sockfd_,(sockaddr*)localAddr.getSockAddr(),sizeof(sockaddr_in)))
    {
        LOG_FATAL("bind sockfd:%d fail\n",sockfd_);
    }
    return 0;
}

void Socket::listen()
{
    if(0 != ::listen(sockfd_,1024))
    {
        LOG_FATAL("listen sockfd%d fail\n",sockfd_);
    }
}

int Socket::accept(InetAddress *peeraddr)
{
    struct sockaddr_in addr;
    socklen_t len;
    bzero(&addr,sizeof addr);
    int connfd = ::accept(sockfd_,(sockaddr*)&addr,&len);
    if(connfd >= 0)
    {
        peeraddr->setSockaddr(addr);
    }
    
    return connfd;
}

void Socket::shutdownWrite()
{
    if(::shutdown(sockfd_,SHUT_WR) < 0)
    {
        LOG_ERROR("shutdownWrite error\n");
    }
}

void Socket::setTcpNoDelay(bool on)
{
    int optval = on ? 1 : 0;
    ::setsockopt(sockfd_,IPPROTO_TCP,TCP_NODELAY,&optval,static_cast<socklen_t>(sizeof(optval)));
}

void Socket::setReuseAddr(bool on)
{
    int optval = on ? 1 : 0;
    ::setsockopt(sockfd_,SOL_SOCKET,SO_REUSEADDR,&optval,static_cast<socklen_t>(sizeof(optval)));
}

void Socket::setReusePort(bool on)
{
    int optval = on ? 1 : 0;
    int ret = ::setsockopt(sockfd_,SOL_SOCKET,SO_REUSEPORT,&optval,static_cast<socklen_t>(sizeof(optval)));
}

void Socket::setKeepAlive(bool on)
{
    int optval = on ? 1 : 0;
    int ret = ::setsockopt(sockfd_,SOL_SOCKET,SO_KEEPALIVE,&optval,static_cast<socklen_t>(sizeof(optval)));
}
